package com.cskaoyan.service;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.*;
import com.cskaoyan.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketCartMapper cartMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketCouponUserMapper couponUserMapper;
    @Override
    @Transactional
    public BaseRespVo orderSubmit(Map<String, Object> map,Map<String,Object> checkoutdata) {
        //注;只需要设置用户可以设置的参数
        MarketOrder order = new MarketOrder();
        MarketAddress checkedAddress = (MarketAddress) checkoutdata.get("checkedAddress");
        List<MarketCart> checkedGoodsList = (List<MarketCart>) checkoutdata.get("checkedGoodsList");
        order.setUserId(checkedAddress.getUserId());
        //根据日期生成ordersn
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String ordersn = sdf.format(new Date());
        order.setOrderSn(ordersn);
        order.setOrderStatus((short) 101);
        order.setAftersaleStatus((short) 0);
        order.setConsignee(checkedAddress.getName());
        order.setMobile(checkedAddress.getTel());
        order.setAddress(checkedAddress.getProvince()+checkedAddress.getCity()+checkedAddress.getCounty()+checkedAddress.getAddressDetail());
        order.setMessage((String) map.get("message"));
        order.setGoodsPrice((BigDecimal) checkoutdata.get("goodsTotalPrice"));
        order.setFreightPrice((BigDecimal) checkoutdata.get("freightPrice"));
        order.setCouponPrice((BigDecimal) checkoutdata.get("couponPrice"));
        order.setIntegralPrice(BigDecimal.ZERO);
        order.setGrouponPrice((BigDecimal) checkoutdata.get("grouponPrice"));
        order.setOrderPrice((BigDecimal) checkoutdata.get("orderTotalPrice"));
        order.setActualPrice((BigDecimal) checkoutdata.get("actualPrice"));
        order.setComments((short) 0);
        order.setAddTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        order.setDeleted(false);
        orderMapper.insertSelective(order);

        //ordergoods表设置
        for (MarketCart cart : checkedGoodsList) {
            MarketOrderGoods orderGoods = new MarketOrderGoods();
            orderGoods.setOrderId(order.getId());
            orderGoods.setGoodsId(cart.getGoodsId());
            orderGoods.setGoodsName(cart.getGoodsName());
            orderGoods.setGoodsSn(cart.getGoodsSn());
            orderGoods.setProductId(cart.getProductId());
            orderGoods.setNumber(cart.getNumber());
            orderGoods.setPrice(cart.getPrice());
            orderGoods.setSpecifications(cart.getSpecifications());
            orderGoods.setPicUrl(cart.getPicUrl());
            orderGoods.setComment(0);
            orderGoods.setAddTime(order.getAddTime());
            orderGoods.setUpdateTime(order.getUpdateTime());
            orderGoods.setDeleted(false);
            orderGoodsMapper.insertSelective(orderGoods);
            //删除购物车中记录
            cart.setDeleted(true);
            cartMapper.updateByPrimaryKey(cart);
            //减库存
            MarketGoodsProduct product = productMapper.selectByPrimaryKeySelective(cart.getProductId());
            Integer stock =product .getNumber();
            Integer updateNumber=stock-cart.getNumber();
            if (updateNumber < 0) {
                return BaseRespVo.fail(cart.getGoodsName() + "库存不足！");
            } else {
                product.setNumber(updateNumber);
                productMapper.updateByPrimaryKey(product);
            }
        }
        Integer userCouponId = (Integer) map.get("userCouponId");
        if ( userCouponId>0) {
            MarketCouponUser couponUser = couponUserMapper.selectByPrimaryKey(userCouponId);
            couponUser.setStatus((short) 1);
            couponUser.setUsedTime(LocalDateTime.now());
            couponUser.setOrderId(order.getId());
            couponUserMapper.updateByPrimaryKey(couponUser);
        }

        Map<String,Object> data=new HashMap<>();
        data.put("gruoponLinkedId",map.get("gruoponLinkedId"));
        data.put("orderId",order.getId());
        return BaseRespVo.ok(data);
    }
}